package leetcode.easy;

public class $07_IntegerReverse {
    //todo:完成07题
    public int reverse(int x){
        int flag = 1;
        if(x < -1 * Math.pow(2, 31) || x > Math.pow(2, 31) - 1)
            return 0;
        if (x == 0) {
            return 0;
        }
        String input = String.valueOf(x);
        if(x < 0) {
            input = input.substring(1, input.length());
            flag = -1;
        }
        for (int i = input.length() - 1; i >= 0; i--){
            if (input.charAt(i) == '0') {
                input = input.substring(0, i);
            } else {
                break;
            }
        }
        char[] chars = input.toCharArray();
        for(int j = 0;j < chars.length / 2;j++){
            char temp = chars[j];
            chars[j] = chars[chars.length - j - 1];
            chars[chars.length - j - 1] = temp;
        }
        String res = String.copyValueOf(chars);
        return flag * (int)Double.parseDouble(res);
    }
}
